﻿Imports System.Data.OleDb


Public Class CKetNoi

    Public Shared ConnecString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DLVip.mdb"
    Public Shared oldbConnection As OleDbConnection
    'mo ket noi
    Public Shared Sub OpenConnection()
        Try
            If (oldbConnection Is Nothing) Then
                oldbConnection = New OleDbConnection(ConnecString)
            End If
            oldbConnection.Open()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub
    Public Shared Sub CloseConnection()

        If (Not oldbConnection Is Nothing) Then
            oldbConnection.Close()
        End If
    End Sub
    Public Shared Function ExecQuery(ByVal sql As String) As DataTable
        Dim dt As DataTable = New DataTable()
        Try
            OpenConnection()

            Dim command As OleDbCommand = oldbConnection.CreateCommand()
            command.CommandText = sql
            Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
            adapter.SelectCommand = command
            adapter.Fill(dt)

        Catch ex As Exception
        Finally
            CloseConnection()
        End Try
       
        Return dt
    End Function

    Public Shared Function ExecnonQuery(ByVal sql As String) As Integer
        Dim kq As Integer = -1
        Try
            OpenConnection()
            Dim Command As OleDbCommand = oldbConnection.CreateCommand()
            Command.CommandText = sql
            kq = Command.ExecuteNonQuery()


        Catch ex As Exception
        Finally
            CloseConnection()
        End Try
        Return kq
      
    End Function
    'ExecuteScalar
    Public Shared Function ExecuteScalar(ByVal sql As String) As Integer
        Dim kq As Integer = -1
        Try
            OpenConnection()
            Dim Command As OleDbCommand = oldbConnection.CreateCommand()
            Command.CommandText = sql
            kq = Command.ExecuteScalar()


        Catch ex As Exception
        Finally
            CloseConnection()
        End Try
        Return kq

    End Function
End Class
